package GUitest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTextFieldDemo extends JFrame {
    public static void main(String[] args) {
        new JTextFieldDemo();
    }

    JButton b;
    JTextField user;
    JTextField msg;
    JTextArea a_msg;

    public JTextFieldDemo(){
        a_msg = new JTextArea(10, 30);
        JScrollPane msgPane = new JScrollPane(a_msg);
        a_msg.setEditable(false);
        JPanel sendPanel = new JPanel();
        user = new JTextField(6);
        msg = new JTextField(20);
        b = new JButton("发送");

        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String u = user.getText();
                String m = msg.getText();
                m = u + ":" + m + "\n";
                a_msg.append(m);
                user.setText("");
                msg.setText("");
            }
        });

        JLabel l1 = new JLabel("用户名:");
        JLabel l2 = new JLabel("留言:");
        sendPanel.add(l1);
        sendPanel.add(user);
        sendPanel.add(l2);
        sendPanel.add(msg);
        sendPanel.add(b);
        this.add(msgPane, BorderLayout.CENTER);
        this.add(sendPanel, BorderLayout.SOUTH);
        this.setTitle("留言板");
        this.setBounds(400, 400, 500 ,200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
